package com.example.teamtribe.main.club.moment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.teamtribe.R;
import com.example.teamtribe.database.Posting;
import com.example.teamtribe.main.club.posting.PostingActivity;
import com.example.teamtribe.main.club.vote.VoteActivity;

import java.util.List;

public class MomentAdapter extends RecyclerView.Adapter<MomentAdapter.ViewHolder> {

    private Context context;
    private List<Posting> postings;
    private int teamId;
    private OnItemClickListener listener;

    public MomentAdapter(List<Posting> postings, int teamId) {
        this.listener = listener;
        this.postings = postings;
        this.teamId = teamId;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.item_posting, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                toInfo(position, false);
            }
        });
        view.findViewById(R.id.tv_like).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点赞
            }
        });
        view.findViewById(R.id.tv_comment).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                toInfo(position, true);
            }
        });
        view.findViewById(R.id.tv_forward).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //转发
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Posting posting = postings.get(position);
        holder.tvTitle.setText(posting.getTitle());
        holder.tvContent.setText(Html.fromHtml(posting.getContent()));
    }

    @Override
    public int getItemCount() {
        return postings.size();
    }

    private void toInfo(int position, boolean isComment) {
        Posting posting = postings.get(position);
        if (posting.getRealId() == 123456789) {
            context.startActivity(new Intent(context, VoteActivity.class));
            return;
        }
        Intent intent = new Intent(context, PostingActivity.class);
        intent.putExtra("posting", postings.get(position));
        intent.putExtra("teamId", teamId);
        intent.putExtra("isComment", isComment);
        ((Activity) context).startActivityForResult(intent, MomentActivity.REQUEST_CHECK);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        TextView tvTitle;
        TextView tvContent;

        public ViewHolder(View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_title);
            tvContent = itemView.findViewById(R.id.tv_content);
        }
    }

    public interface OnItemClickListener {

        void comment(int targetId);
    }
}